package com.woniuxy.controller;


import com.woniuxy.entity.DTO.AddressDTO;
import com.woniuxy.entity.DTO.DeleteBatchShoppingCartGoodsDTO;
import com.woniuxy.entity.DTO.QueryShoppingCartGoodsDTO;
import com.woniuxy.entity.ShoppingCarts;
import com.woniuxy.service.ShoppingCartService;
import com.woniuxy.util.ResponseData;
import com.woniuxy.util.ResponseEnum;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {

    @Resource
    private ShoppingCartService shoppingCartService;
    @GetMapping("/buyAgain/{orderId}")
    public ResponseData buyAgain(@PathVariable String orderId) {
        try {
            if (!shoppingCartService.buyAgain(orderId)){
                return ResponseData.fail(ResponseEnum.ADD_SHOPPING_CART_FAIL);
            }
            return ResponseData.success();
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }
    @PostMapping("/addShoppingCart")
    public ResponseData addShoppingCart(@RequestBody ShoppingCarts shoppingCarts) {
        try {
            if (!shoppingCartService.addShoppingCartByProductId(shoppingCarts)){
                return ResponseData.fail(ResponseEnum.ADD_SHOPPING_CART_FAIL);
            }
            return ResponseData.success();
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }

    }

    /**
     * <p>查询所有购物车信息。</p>
     *
     * @return 购物车信息列表
     */
    @GetMapping("/queryShoppingCart")
    public ResponseData<?> queryShoppingCart() {
        List<QueryShoppingCartGoodsDTO> shoppingCarts;
        try {
            shoppingCarts = shoppingCartService.queryShoppingCart();
        } catch (ParseException e) {
            return ResponseData.fail(ResponseEnum.FAIL);
        }
        return ResponseData.success(shoppingCarts);
    }

    /**
     * <p>批量删除用户选中的购物车商品</p>
     * @param deleteBatchShoppingCartGoodsDTOs 用户选中的购物车商品
     * @return 是否删除成功
     */
    @PostMapping("/deleteBatchShoppingCart")
    public ResponseData<?> deleteBatchShoppingCart(@RequestBody List<DeleteBatchShoppingCartGoodsDTO> deleteBatchShoppingCartGoodsDTOs) {
        try {
            shoppingCartService.deleteBatchShoppingCartGoods(deleteBatchShoppingCartGoodsDTOs);
        } catch (ParseException e) {
            return ResponseData.fail(ResponseEnum.FAIL);
        }
        return ResponseData.success();
    }

    /**
     * <p>添加普通商品到购物车</p>
     * @param goodsId
     * @return
     */
    @GetMapping("/addShoppingCartForCommonGoods")
    public ResponseData<?> addShoppingCartForCommonGoods(@RequestParam("goodsId") Integer goodsId) {
        try {
            shoppingCartService.addShoppingCartForCommonGoods(goodsId);
        } catch (ParseException e) {
            return ResponseData.fail(ResponseEnum.FAIL);
        }
        return ResponseData.success();
    }

    /**
     * <p>查询用户默认收货地址</p>
     * @return
     */
    @GetMapping("/queryDefaultAddress")
    public ResponseData<?> queryDefaultAddress() {
        AddressDTO addressDTO;
        try {
            addressDTO = shoppingCartService.queryDefaultAddress();
        } catch (ParseException e) {
            return ResponseData.fail(ResponseEnum.FAIL);
        }
        return ResponseData.success(addressDTO);
    }
}
